package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RequestMapping("/setMeal")
@Controller
@ResponseBody
public class SetMealController {

    @Reference
    private SetMealService setMealService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile){
        boolean flag=false;
        String message=null;
        String filename = imgFile.getOriginalFilename();
        int index = filename.lastIndexOf(".");
        String imgFileName= UUID.randomUUID()+filename.substring(index);
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),imgFileName);
            flag=true;
            message= MessageConstant.PIC_UPLOAD_SUCCESS;
            imgFileName="http://q1ys9h7cx.bkt.clouddn.com/"+imgFileName;
            //将图片名称存入redis
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,imgFileName);
        } catch (IOException e) {
            message= MessageConstant.PIC_UPLOAD_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message,imgFileName);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[]checkgroupIds){
        boolean flag=false;
        String message=null;
        try {
            setMealService.add(setmeal,checkgroupIds);
            flag=true;
            message=MessageConstant.ADD_SETMEAL_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.ADD_SETMEAL_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message);
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        boolean flag=false;
        String message=null;
        PageResult pageResult= null;
        try {
            pageResult = setMealService.findPage(queryPageBean);
            flag=true;
            message=MessageConstant.QUERY_SETMEALLIST_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.QUERY_SETMEALLIST_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message,pageResult);
    }

    @RequestMapping("/findSetMealById")
    public Result findSetMealById(Integer id){
        boolean flag=false;
        String message=null;
        Setmeal setmeal= null;
        try {
            setmeal = setMealService.findSetMealById(id);
            flag=true;
            message=MessageConstant.QUERY_SETMEAL_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.QUERY_SETMEAL_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message,setmeal);
    }

    @RequestMapping("/findSetMealAndCheckGroupById")
    public Result findSetMealAndCheckGroupById(Integer id){
        boolean flag=false;
        String message=null;
        List<Integer>integerList=null;
        try {
            integerList=setMealService.findSetMealAndCheckGroupById(id);
            flag=true;
            message=MessageConstant.QUERY_CHECKGROUP_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.QUERY_CHECKGROUP_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message,integerList);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Setmeal setmeal,Integer[]checkgroupIds){
        boolean flag=false;
        String message=null;
        try {
            setMealService.update(setmeal,checkgroupIds);
            flag=true;
            message=MessageConstant.EDIT_SETMEAL_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.EDIT_SETMEAL_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message);
    }

    @RequestMapping("/del")
    public Result del(@RequestBody Setmeal setmeal){
        String message=null;
        boolean flag=false;
        Integer id = setmeal.getId();
        String img = setmeal.getImg();
        try {
            setMealService.del(id,img);
            flag=true;
            message=MessageConstant.DELETE_SETMEAL_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.DELETE_SETMEAL_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message);
    }
}
